package com.nf.health.app.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.nf.health.app.R;

import java.util.ArrayList;
import java.util.List;

public class ScoreBar extends LinearLayout {
	private int MaxNum = 5;
	private List<ImageView> images = new ArrayList<ImageView>();
	private int full_drawable = android.R.drawable.btn_star_big_on, half_drawable = android.R.drawable.btn_star_big_off, empty_drawable = android.R.drawable.star_on;
	private int imageSize = 40;

	public ScoreBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub

		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScoreBar);
		MaxNum = typedArray.getInteger(R.styleable.ScoreBar_MaxNum, 5);
		full_drawable = typedArray.getResourceId(R.styleable.ScoreBar_full_drawable, android.R.drawable.btn_star_big_on);
		half_drawable = typedArray.getResourceId(R.styleable.ScoreBar_half_drawable, half_drawable = android.R.drawable.btn_star_big_off);
		empty_drawable = typedArray.getResourceId(R.styleable.ScoreBar_empty_drawable, android.R.drawable.star_on);
		imageSize = typedArray.getDimensionPixelOffset(R.styleable.ScoreBar_imagesize, 30);
		layoutImages(context);
		setOrientation(LinearLayout.HORIZONTAL);
	}

	public ScoreBar(Context context) {
		super(context);
		layoutImages(context);
		setOrientation(LinearLayout.HORIZONTAL);
		// TODO Auto-generated constructor stub
	}

//	@SuppressLint("NewApi")
//	public ScoreBar(Context context, AttributeSet attrs, int defStyle) {
//		super(context, attrs, defStyle);
//		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScoreBar);
//		MaxNum = typedArray.getInteger(R.styleable.ScoreBar_MaxNum, 5);
//		full_drawable = typedArray.getResourceId(R.styleable.ScoreBar_full_drawable, android.R.drawable.btn_star_big_on);
//		half_drawable = typedArray.getResourceId(R.styleable.ScoreBar_half_drawable, half_drawable = android.R.drawable.btn_star_big_off);
//		empty_drawable = typedArray.getResourceId(R.styleable.ScoreBar_empty_drawable, android.R.drawable.star_on);
//		imageSize = typedArray.getDimensionPixelOffset(R.styleable.ScoreBar_imagesize, 30);
//		layoutImages(context);
//		setOrientation(LinearLayout.HORIZONTAL);
//	}

	public void layoutImages(Context context) {
		for (int i = 0; i < MaxNum; i++) {
			ImageView imageView = new ImageView(context);
			LayoutParams layoutParams = new LayoutParams(imageSize, imageSize);
			layoutParams.gravity = Gravity.CENTER_VERTICAL;
			imageView.setLayoutParams(layoutParams);
			imageView.setImageResource(empty_drawable);
			ScoreBar.this.addView(imageView);
			images.add(imageView);
		}
	}

	public void setMax(int max, Context context) {
		MaxNum = max;
		images.clear();
		layoutImages(context);
	}

	public void setNum(Double num) {
		System.err.println("------num---" + num);
		for (int i = 0; i < MaxNum; i++) {
			images.get(i).setImageResource(empty_drawable);
		}
		int index = 0;
		if (num > 0) {
			for (int i = 0; i < num; i++) {
				images.get(i).setImageResource(full_drawable);
				index = i;
			}
			if (index + 1 > num ) {
				images.get(index).setImageResource(half_drawable);
			}
		}
	}

	public void setNum(int num) {
		for (int i = 0; i < MaxNum; i++) {
			images.get(i).setImageResource(empty_drawable);
		}
		for (int i = 0; i < num; i++) {
			images.get(i).setImageResource(full_drawable);
		}
	}

}
